8963. Minimums to the left

 

Given array of n integers. Move all minimum elements to the beginning of the array without changing the order of others.

 

Input. The first line contains positive integer n (n 100). Next line contains n integers. All numbers do not exceed 100 by absolute value.

 

Output. Print the elements of updated array.

 

Sample input

Sample output

7

6 -3 -7 4 -7 -4 5

-7 -7 6 -3 4 -4 5

 

 

SOLUTION

array

 

Algorithm analysis

Find the minimum element min. Move along the array from right to left and move non-minimal elements to the right. The part of the array that remains to the left is filled with the minimum element.

 

Example

Consider an example given in problem statement.

 

 

Algorithm realization

Declare an array.

 

int m[101];

 

Read the input array. Find the minimum element min.

 

scanf("%d", &n);

min = 100;

for (i = 0; i < n; i++)

{

  scanf("%d", &m[i]);

  if (m[i] < min) min = m[i];

}

 

Declare two indices i and j. Move through the array from right to left. If m[j] is not equal to the minimum, then copy this number to m[i].

 

i = n - 1;

for (j = n - 1; j >= 0; j--)

  if (m[j] != min)

  {

    m[i] = m[j];

    i--;

  }

 

The rest of the array elements from index 0 to i should be filled with minimum element.

 

  while (i >= 0)

  {

    m[i] = min;

    i--;

  }

 

Print the resulting array.

 

for (i = 0; i < n; i++)

  printf("%d ", m[i]);

printf("\n");